package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

public class ViewCategoryServlet extends HttpServlet {
    private static final String VIEW_CATEGORY="/WEB-INF/jsp/catalog/Category.jsp";

    private String categoryId;


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        categoryId=request.getParameter("categoryId");
        CatalogService service=new CatalogService();
        Category category= service.getCategory(categoryId);
        List<Product> productList=service.getProductListByCategory(categoryId);
        HttpSession session=request.getSession();
        session.setAttribute("category",category);
        session.setAttribute("productList",productList);

        System.out.println(session.getAttribute("category"));

        request.getRequestDispatcher(VIEW_CATEGORY).forward(request,response);

    }
}
